package org.bees.cms.web.util.velocity.editer;

import java.io.Serializable;

import net.sf.json.JSONObject;

import org.bees.cms.web.util.Beans;

public class ArrayEditer extends AbstractEditer {
	private String container;
	private String label;

	@Override
	public void initialize(JSONObject json) throws Exception {
		super.initialize(json);
		container = json == null ? container : json.containsKey("container") ? json.getString("container") : container;
		label = json == null ? label : json.containsKey("label") ? json.getString("label") : label;
	}

	@Override
	public String edit(Serializable t) throws Exception {
		StringBuilder builder = new StringBuilder();

		container = container == null || container.trim().equals("") ? name : container;
		builder.append("<div id='" + container + "'></div>");

		builder.append("<button");
		builder.append(" type='button'");
		builder.append(" onclick=\"javascript:addInputOption('" + container + "','" + label + "','" + name + "','','" + style + "','" + validator + "');\"");
		builder.append(">");
		builder.append("添加");
		builder.append("</button>");

		builder.append("<script type='text/javascript'>");
		Object[] array = t == null ? new Object[] {} : (Object[]) Beans.get(t, property);
		for (Object object : array) {
			builder.append("addInputOption('" + container + "','" + label + "','" + name + "','" + object + "','" + style + "');");
		}
		builder.append("</script>");

		return builder.toString();
	}

	@Override
	public void destroy() throws Exception {
		super.destroy();
		container = null;
		label = null;
	}

	public String getContainer() {
		return container;
	}

	public void setContainer(String container) {
		this.container = container;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

}
